/**
 * 
 */
package ads.engenharia.ui;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import ads.engenharia.bl.CourseBO;
import ads.engenharia.entity.Course;
import ads.engenharia.iface.IHaveFields;
import ads.engenharia.model.CourseTableModel;

/**
 * @author Lanedraex
 *
 */
public class QueryCourseView extends JPanel implements IHaveFields {

	JButton btn_search;
	Thread nameThread;
	Thread durationThread;
	boolean isAllRight;

	private static final long serialVersionUID = 1283050030934063651L;
	private JTextField txt_name;
	private JTextField txt_duration;
	private JTextField textField;
	private JTable tb_queryTable;

	public QueryCourseView() {
		setLayout(null);

		JLabel lbl_id = new JLabel("Codigo");
		lbl_id.setBounds(10, 11, 33, 14);
		add(lbl_id);

		txt_name = new JTextField();

		txt_name.setBounds(53, 8, 79, 20);
		add(txt_name);
		txt_name.setColumns(50);

		JLabel lbl_name = new JLabel("Nome");
		lbl_name.setBounds(142, 11, 27, 14);
		add(lbl_name);

		txt_duration = new JTextField();
		txt_duration.setColumns(10);
		txt_duration.setBounds(179, 8, 173, 20);
		add(txt_duration);

		btn_search = new JButton("Procurar");
		btn_search.setBounds(699, 7, 91, 23);
		add(btn_search);
		btn_search.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				CourseBO courseBO = new CourseBO();
				ArrayList<Course> courses;
				try {
					courses = courseBO.searchByName(txt_name.getText());
					CourseTableModel tableModel = new CourseTableModel(courses);
					tb_queryTable.setModel(tableModel);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});

		JLabel lbl_duration = new JLabel("Dura\u00E7\u00E3o");
		lbl_duration.setBounds(362, 11, 40, 14);
		add(lbl_duration);

		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(412, 8, 79, 20);
		add(textField);

		JLabel lbl_hasDisciplines = new JLabel("Disciplinas");
		lbl_hasDisciplines.setBounds(501, 11, 48, 14);
		add(lbl_hasDisciplines);

		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Sim",
				"N\u00E3o" }));
		comboBox.setMaximumRowCount(10);
		comboBox.setBounds(559, 7, 79, 22);
		add(comboBox);

		JScrollPane scrp_queryPanel = new JScrollPane();
		scrp_queryPanel.setBounds(10, 286, 780, -235);
		add(scrp_queryPanel);

		tb_queryTable = new JTable();
		// tb_queryTable.setBounds(0, 0, 1, 1);
		// add(tb_queryTable);
		tb_queryTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				int r = tb_queryTable.rowAtPoint(e.getPoint());
				if (r >= 0 && r < tb_queryTable.getRowCount()) {
					tb_queryTable.setRowSelectionInterval(r, r);
				} else {
					tb_queryTable.clearSelection();
				}

				int rowindex = tb_queryTable.getSelectedRow();
				if (rowindex < 0)
					return;
				if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
					JPopupMenu popup = generatePopup();
					popup.show(e.getComponent(), e.getX(), e.getY());
				}
			}
		});
		scrp_queryPanel.setViewportView(tb_queryTable);

	}

	private JPopupMenu generatePopup() {

		JPopupMenu popup = new JPopupMenu();
		ActionListener menuListener = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.out.println("Popup menu item ["
						+ event.getActionCommand() + "] was pressed.");
			}
		};

		JMenuItem item;
		popup.add(item = new JMenuItem("Alterar"));
		item.setHorizontalTextPosition(JMenuItem.LEFT);
		item.addActionListener(menuListener);
		popup.add(item = new JMenuItem("Remover"));
		item.setHorizontalTextPosition(JMenuItem.LEFT);
		item.addActionListener(menuListener);

		return popup;
	}

	@Override
	public boolean checkField(Object field) {
		// if (txt_name.getText().trim().isEmpty()
		// || txt_name.getText().length() > 50) {
		//
		// if (txt_duration.getText().trim().isEmpty()) {
		//
		// return false;
		// }
		// }

		if (field instanceof TextField) {
			if (((TextField) field).getText().trim().isEmpty()) {
				isAllRight = false;
				return false;
			}
		}

		isAllRight = true;
		return true;
	}
}
